import 'package:flutter/material.dart';
import 'package:chathouse/public/Config.dart';

abstract class BaseStatefulWidget extends StatefulWidget {
  const BaseStatefulWidget({Key? key}) : super(key: key);

  @override
  BaseStatefulWidgetState createState() => BaseStatefulWidgetState();
}

class BaseStatefulWidgetState<BaseStatefulWidget> extends State {
  // appbar 的显示与否
  bool showAppBar = false;

  bool isShowErrorBackBtn = false;

  Widget? leading;


  //右边标题
  List<Widget> actions = [];

  // 页面标题
  String appBarTitle = '';

  Color scaffoldBackColor = Colors.white;
  Color appBarBackColor = Colors.white;

  // appbarView 的高度
  double appBarHeight = 44;

  late LoadLayoutPage loadingView;
  LoadState lState = LoadState.loading;

  void updateState(LoadState value) {
    lState = value;
    if (mounted) {
      setState(() {});
    }
  }

  void updateEmptyPage(
      {VoidCallback? emptyRetry, String? imgPath, String? tipTitle}) {
    loadingView.imgPath = imgPath;
    loadingView.tipTitle = tipTitle;
    loadingView.emptyRetry = emptyRetry;
  }

  void updateErrorPage({VoidCallback? errorRetry}) {
    loadingView.errorRetry = errorRetry;
  }

  /// 配置页面头部标题
  Widget AppBarTitle() {
    return Text(
      appBarTitle,
      style: const TextStyle(color: Styles.c_333, fontSize: 22,fontWeight: FontWeight.w600),
    );
  }
  
  /// 配置页面头部内容
  AppBar viewAppBar() {
    return AppBar(
        toolbarHeight: appBarHeight,
        title: AppBarTitle(),
        backgroundColor: appBarBackColor,
        leading: IconButton(
          icon: leading ?? Image.asset(Res.expert_back),
          onPressed: () {
            Navigator.of(context).pop();
            returnCallBack();
          },
        ),
        leadingWidth: 48,
        centerTitle: true,
        actions: actions);
  }

  void returnCallBack() {}

  /// 页面视图的主体部分（这里不要写_viewPageBody。不要加下划线，加了就是私有的，不能override了）
  Widget viewPageBody() {
    return Container();
  }

  @override
  void initState() {
    super.initState();
    loadingView = LoadLayoutPage(state: lState);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // resizeToAvoidBottomInset: false, //输入框抵住键盘
      backgroundColor: scaffoldBackColor,
      appBar: showAppBar == true ? viewAppBar() : null,
      body: lState != LoadState.success
          ? LoadLayoutPage(
              state: lState,
              isShowBackBtn: isShowErrorBackBtn,
              imgPath: loadingView.imgPath,
              tipTitle: loadingView.tipTitle,
              emptyRetry: loadingView.emptyRetry,
              errorRetry: loadingView.errorRetry,
            )
          : SafeArea(
              child: GestureDetector(
              child: viewPageBody(),
            )),
    );
  }
}
